package com.jinzecloud.blog.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.jinzecloud.blog.bean.Article;
import com.jinzecloud.blog.bean.Detail;
import com.jinzecloud.blog.service.ArticleService;
import com.jinzecloud.blog.utils.Page;
import com.jinzecloud.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文章
 *
 * @author kumi
 * @email 1971240051@qq.com
 * @date 2022-06-04 03:23:06
 */
@RestController
@RequestMapping("article/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @RequestMapping("/add")
    public Result addTag(@RequestBody Article article) {
        try {
            articleService.add(article);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "添加失败");
        }

        return Result.ok().put("200", "添加成功");
    }

    @RequestMapping("/update")
    public Result updateArticle(@RequestBody Article article) {
        try {
            articleService.updateArticle(article);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "修改失败");
        }

        return Result.ok().put("200", "修改成功");
    }

    @RequestMapping("/delete")
    public Result deleteArticle(@RequestBody Long[] ids) {
        try {
            articleService.deleteArticle(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "修改失败");
        }

        return Result.ok().put("200", "修改成功");
    }

    @RequestMapping("/getById/{id}")
    public Result getByIdArticle(@PathVariable Long id) {
        Article article = null;
        try {
            article = articleService.getByIdArticle(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "查询失败");
        }

        return Result.ok().put("data", article);
    }

    @RequestMapping("/getAll")
    public Result getAll() {
        Page<Article> page = null;
        try {
            page = articleService.getAllArticle();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "查询失败");
        }

        return Result.ok().put("data", page);
    }

    /**
     * 修改点赞数量
     *
     * @return
     */
    @RequestMapping("/updateArticleLikecount")
    public Result updateArticleLikecount(@RequestBody Article article) {
        try {
            articleService.updateArticleLikecount(article);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "点赞修改失败");
        }

        return Result.ok().put("200", "点赞修改成功");
    }

    /**
     * 修改评论数量
     *
     * @return
     */
    @RequestMapping("/updateArticleCommentcount")
    public Result updateArticleCommentcount(@RequestBody Article article) {
        try {
            articleService.updateArticleCommentcount(article);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "点赞修改失败");
        }

        return Result.ok().put("200", "点赞修改成功");
    }

    /**
     * 修改浏览数量
     *
     * @return
     */
    @RequestMapping("/updateArticleReadcount")
    public Result updateArticleReadcount(@RequestBody Article article) {
        try {
            articleService.updateArticleReadcount(article);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "浏览修改失败");
        }

        return Result.ok().put("200", "浏览修改成功");
    }


    /**
     * 修改是否置顶
     *
     * @return
     */
    @RequestMapping("/updateArticleTopflag")
    public Result updateArticleTopflag(@RequestBody Article article) {
        try {
            articleService.updateArticleTopflag(article);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "置顶修改失败");
        }

        return Result.ok().put("200", "置顶修改成功");
    }
}
